import request from '@/utils/request'
import Qs from 'qs'

export function createController(data) {
  return request({
    url: '/controller.dev/create',
    method: 'post',
    data: Qs.stringify(data)
  })
}

export function listController(productId) {
  return request({
    url: '/controller.dev/list',
    method: 'get',
    params: { productId }
  })
}

export function listComponent(controllerId) {
  return request({
    url: '/controller.dev/listComponent',
    method: 'get',
    params: { controllerId }
  })
}

export function deleteComponent(componentId) {
  return request({
    url: '/controller.dev/deleteComponent',
    method: 'post',
    data: Qs.stringify({ componentId })
  })
}

export function createComponent(data) {
  return request({
    url: '/controller.dev/createComponent',
    method: 'post',
    data: Qs.stringify({
      styleMode: data.styleMode,
      style: data.style,
      controllerId: data.controllerId,
      componentLibId: data.componentLibId,
      title: data.title,
      sort: data.sort,
      attrList: JSON.stringify(data.attrList)
    })
  })
}

export function editComponent(data) {
  return request({
    url: '/controller.dev/editComponent',
    method: 'post',
    data: Qs.stringify({
      styleMode: data.styleMode,
      style: data.style,
      componentId: data.componentId,
      title: data.title,
      attrList: JSON.stringify(data.attrList)
    })
  })
}

export function adjustComponentSort(sortMap) {
  return request({
    url: '/controller.dev/adjustSort',
    method: 'post',
    data: Qs.stringify({ sortMap: JSON.stringify(sortMap) })
  })
}

export function listComponentAttr(componentId) {
  return request({
    url: '/controller.dev/listComponentAttr',
    method: 'get',
    params: { componentId }
  })
}

export function listLibraryAttr(libId) {
  return request({
    url: '/controller.dev/listLibAttr',
    method: 'get',
    params: { libId }
  })
}

export function listLibrary() {
  return request({
    url: '/controller.dev/listLib',
    method: 'get'
  })
}

